//
//  LPTMineViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/3.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTMineViewController.h"
#import "UserHeaderCell.h"
#import "SegmentViewCell.h"
#import "MineCell.h"
#import "LPTBaseRequest.h"
#import "UserInfoModel.h"
#import "LPTLoginViewController.h"
#import "LPTWebViewController.h"
#import "MyOutpatientTableViewController.h"
#import "MyInviteTableViewController.h"
#import "HelpCenterTableViewController.h"
#import "AboutUsTableViewController.h"
#import "PersonUserInfoTableViewController.h"

@interface LPTMineViewController()

@property (nonatomic, copy) NSArray *list;
@property (nonatomic, strong) UserInfoModel *userInfoModel;
@end


@implementation LPTMineViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    UserInfoModel *model = [LTPCacheManager shareInstance].getUserInfoModel;
    if (model) {
        self.userInfoModel = model;
        [self setUpPageData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.tableFooterView = [UIView new];
    
    self.list = @[@{ @"image": @"my_icon_department", @"name": @"我的门诊"},
                       @{ @"image": @"my_icon_invitation", @"name": @"我的邀请"},
                       @{ @"image": @"my_icon_help", @"name": @"帮助中心"},
                       @{ @"image": @"my_icon_about", @"name": @"关于我们"},
                       @{ @"image": @"my_icon_privacy", @"name": @"隐私政策"}];
    
    [self requestUserInfo];
}

- (void)requestUserInfo
{
    NSString *apiName = @"/api/doctor_info";
    
    UserInfoModel *model = [LTPCacheManager shareInstance].getUserInfoModel;
    if (model) {
        self.userInfoModel = model;
        [self setUpPageData];
    }
    
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:apiName withBody:@{} withMethod:YTKRequestMethodGET];
    
    [request startRequestWithClass:[UserInfoModel class] completeSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        // 缓存用户信息；
//        NSString *json = request.responseString;
        [[LTPCacheManager shareInstance] cacheUserInfo:request.responseObject[@"data"]];
        
        self.userInfoModel = (UserInfoModel *)data;
        [self setUpPageData];
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
    }];
}

- (void)setUpPageData
{
    [self.tableView reloadData];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return self.list.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [[UIView alloc] init];
    v.frame = CGRectMake(0, 0, kScreenWidth, 10);
    v.backgroundColor = [UIColor clearColor];
    return v;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    if (section == 0) {
        UserHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserHeaderCellID" forIndexPath:indexPath];
        [cell refreshWithLogin:[LTPCacheManager shareInstance].token data:self.userInfoModel];
        return cell;
    } else if (section == 1) {
        SegmentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SegmentViewCellID" forIndexPath:indexPath];
        return cell;

    } else if (section == 2) {
        MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCellID" forIndexPath:indexPath];
        
        NSDictionary *dict = [self.list objectAtIndex:row];
        NSString *imgName = dict[@"image"];
        NSString *title = dict[@"name"];
        
        cell.imageView.image = [UIImage imageNamed:imgName];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.text = title;
        
        return cell;

    }
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        // 去用户信息编辑页
        PersonUserInfoTableViewController *vc = [PersonUserInfoTableViewController instance];
        [self.navigationController pushViewController:vc animated:YES];

    }else if (indexPath.section == 2) {
        [self showViewController:indexPath.row];
    }
}

- (void)showViewController:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            MyOutpatientTableViewController *vc = [MyOutpatientTableViewController instance];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            MyInviteTableViewController *vc = [MyInviteTableViewController instance];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            HelpCenterTableViewController *vc = [HelpCenterTableViewController instance];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            AboutUsTableViewController *vc = [AboutUsTableViewController instance];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            LPTWebViewController *vc = [LPTWebViewController instanceWithUrl:@"https://doctor.5ixlz.com/common/article_detail?slug=doctor-protocol"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

@end
